<template>
  <div class="main">
    <div class="header">
      <el-dialog
  :visible.sync="newsDialogVisible"
  width="30%"
  :before-close="closeTime">
  <el-form :model="newsForm">
    <el-form-item label="描述">
    <el-input v-model="newsForm.title"></el-input>
  </el-form-item>
  <el-form-item label="链接">
    <el-input type="textarea" v-model="newsForm.desc"></el-input>
  </el-form-item>
  <el-button type="primary" @click="newsSubmitForm"  plain>提交</el-button>
  </el-form>
</el-dialog>
          <div class="header-l">
            <el-button @click="openAdd" size="mini" type="primary">添加</el-button>
          </div>
          <div class="header-r">
            <el-input placeholder="请输入标题关键字进行搜索" style="width:188px;margin-right:8px" size="mini" v-model="serchVal"></el-input>
            <el-button style="background:skyblue;color:#fff;border:1px solid purple" @click="searchFun" size="mini">搜索</el-button>
          </div>
    </div>
    <div class="body">
      <el-table
    :data="tableData"
    style="width: 100%"
    height="498">
    <el-table-column
      fixed
      prop="n_time"
      label="时间"
      width="180">
    </el-table-column>
    <el-table-column
      prop="n_title"
      label="标题"
      width="280">
    </el-table-column>
    <el-table-column
      prop="n_area"
      label="链接地址"
      width="580">
    </el-table-column>
    <el-table-column
      prop="zip"
      label="操作">
      <template slot-scope="scope">
        <el-button @click="deleteNews(scope.row)" type="text">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
    </div>
    <div class="footer" style="position:fixed;right:0;bottom:0">
      <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-size="10"
      layout="prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </div>
  </div>
</template>
<script>
import { getNews, deleteNews, addNews } from '../api/info'
export default {
  data () {
    return {
      newsDialogVisible: false,
      newsForm: {
        title: '',
        desc: ''
      },
      serchVal: '',
      currentPage: 1,
      total: 0,
      tableData: []
    }
  },
  methods: {
    // 搜索
    searchFun () {
      this.currentPage = 1
      this.getNewsInfo()
    },
    // 关闭视图时
    closeTime () {
      this.newsForm.title = ''
      this.newsForm.desc = ''
      this.newsDialogVisible = false
    },
    // 提交添加
    newsSubmitForm () {
      if (!this.newsForm.title || !this.newsForm.desc) {
        return this.$message.info('内容不能为空')
      }
      addNews({ title: this.newsForm.title, desc: this.newsForm.desc }).then(({ data }) => {
        if (data.status === 200) {
          this.$message.success('添加成功')
          this.newsForm.title = ''
          this.newsForm.desc = ''
          this.newsDialogVisible = false
          this.getNewsInfo()
        }
      })
    },
    // 打开视图
    openAdd () {
      this.newsDialogVisible = true
    },
    // 删除资讯
    deleteNews (row) {
      deleteNews({ n_id: row.n_id }).then(({ data }) => {
        if (data.status === 200) {
          this.$message.success('删除成功')
          this.getNewsInfo()
        }
      })
    },
    // 跳转页
    handleCurrentChange (val) {
      this.currentPage = val
      this.getNewsInfo()
      // console.log(val)
    },
    getNewsInfo () {
      getNews({ serchVal: this.serchVal, currentPage: this.currentPage }).then(({ data }) => {
        // console.log(data)
        this.tableData = data.arrVal
        this.total = data.total
      })
    }
  },
  mounted () {
    this.getNewsInfo()
  }
}
</script>
<style scoped lang="less">
  .header{
    display: flex;
    height: 32px;
    // background: red;
    justify-content: space-between;
    align-items: center;
    /deep/ .el-input__inner{
      background:transparent;
      border:1px solid blue;
    }
  }

</style>
